﻿using BLL;
using DAL;
using DevExpress.Utils.Layout;
using Model;
using Newtonsoft.Json;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 后整理管理系统.其他窗体;
using 后整理管理系统.数据分析;
using 后整理管理系统.自定义类;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.新增窗体;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 纺织贸易管理系统.报表窗体
{
    public partial class 销售发货列表 : Form
    {
        public 销售发货列表()
        {
            InitializeComponent();
            if (SysInfo.GetInfo.own != "审核制")
            {
                单据审核ToolStripMenuItem.Enabled = false;
            }
            dateEdit2.DateTime = DateTime.Now;
            dateEdit1.DateTime = dateEdit2.DateTime.AddDays(-QueryTime.间隔);
            CreateGrid.Create(this.Name, gridView1);
            try
            {
                gridView1.OptionsCustomization.AllowSort = true;
                gridView1.Columns["hanshuidanjia"].Visible = false;
                gridView1.Columns["hanshuiheji"].Visible = false;
                gridView1.Columns["rq"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";//设置时间显示格式
                gridView1.Columns["mashu"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;//设置数字显示格式
                gridView1.Columns["mashu"].DisplayFormat.FormatString = "N2";//设置数字显示格式
            }
            catch
            {
                配置列ToolStripMenuItem_Click(null, null);
            }
            //comboBox1.SelectedIndex = 0;
        }
        public virtual void Query()
        {
            bool amount = false;
            if (string.IsNullOrEmpty(txtksmc.Text))//如果客户名是空的就必须是模糊查询
            { 
                uiCheckBox3.Checked=false ;
            }
           amount=uiCheckBox3.Checked ;
            var querystring = string.Empty;
            if (uiCheckBox2.Checked)
            {
                querystring = $"select danjutable.*,danjumingxitable.*,lxr.FullName from danjutable left join lxr on danjutable.ksmc=lxr.mc,danjumingxitable where danjutable.rq between '{dateEdit1.DateTime.Date}' and '{dateEdit2.DateTime.Date.AddDays(1)}' and danjutable.ksmc  {(!amount ? $"like '%{txtksmc.Text.Replace("'", "''")}%'" : $"='{txtksmc.Text.Replace("'", "''")}'")} " +
                   $"and danjumingxitable.bianhao like '%{txtbianhao.Text.Replace("'", "''")}%' " +
                   $"and danjumingxitable.pingming like '%{txtpingming.Text.Replace("'", "''")}%' " +
                   $"and danjumingxitable.guige like '%{txtGuige.Text.Replace("'", "''")}%' " +
                   $"and danjumingxitable.yanse like '%{txtyanse.Text.Replace("'", "''")}%' " +
                   $"and danjumingxitable.ganghao like '%{txtganghao.Text.Replace("'", "''")}%' " +
                   $"and danjumingxitable.OrderNum like '%{txtordernum.Text.Replace("'", "''")}%' and lxr.lx='客户' " +
                   $"and danjutable.djlx='{DanjuLeiXing.销售出库单}' " +
                   $"and danjutable.dh=danjumingxitable.danhao and danjutable.dh in (select Fahuodan.dh from Fahuodan where Fahuodan.dh=danjutable.dh)";
            }
            else
            {
                querystring = $"select danjutable.*,danjumingxitable.*,lxr.FullName from danjutable left join lxr on danjutable.ksmc=lxr.mc,danjumingxitable where danjutable.rq between '{dateEdit1.DateTime.Date}' and '{dateEdit2.DateTime.Date.AddDays(1)}' and danjutable.ksmc  {(!amount ? $"like '%{txtksmc.Text.Replace("'", "''")}%'" : $"='{txtksmc.Text.Replace("'", "''")}'")} " +
                    $"and danjumingxitable.bianhao like '%{txtbianhao.Text.Replace("'", "''")}%' " +
                    $"and danjumingxitable.pingming like '%{txtpingming.Text.Replace("'", "''")}%' " +
                    $"and danjumingxitable.guige like '%{txtGuige.Text.Replace("'", "''")}%' " +
                    $"and danjumingxitable.yanse like '%{txtyanse.Text.Replace("'", "''")}%' " +
                    $"and danjumingxitable.ganghao like '%{txtganghao.Text.Replace("'", "''")}%' " +
                    $"and danjumingxitable.OrderNum like '%{txtordernum.Text.Replace("'", "''")}%' and lxr.lx='客户' " +
                    $"and danjutable.djlx in ('{DanjuLeiXing.销售出库单}' )" +
                    $"and danjutable.dh=danjumingxitable.danhao ";
            }
            if (Tools.CurrentUser.user.access == "自己")
            {
                querystring += $" and  danjutable.own='{Tools.CurrentUser.user.YHBH}'";
            }
            querystring += " order by danjutable.id desc";
            DataTable dt = Connect.dbcontext.Ado.GetDataTable(querystring);
            gridControl1.DataSource = dt;

        }
        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("新增发货单"))
            {
                MainForm.mainform.AddMidForm(new 销售发货单() { useful = FormUseful.新增 });
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                if (AccessBLL.CheckAccess("修改销售发货单") == true)
                {
                    if (Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString() && x.Reconciliation == true) != null)
                    {
                        Tools.ShowMessageBox.ShowErrorBox("该单据已经存在已对账信息！不能直接修改\r\n请先取消对账状态");
                        return;
                    }
                    if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                    {
                        MainForm.mainform.AddMidForm(new 销售发货单() { useful = FormUseful.修改, danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
                    }
                    else
                    {
                        if (SysInfo.GetInfo.own != "审核制")
                        {
                            MainForm.mainform.AddMidForm(new 销售发货单() { useful = FormUseful.修改, danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
                        }
                        else
                        {
                            MessageBox.Show("对不起!改单据已经审核，如需修改请先取消审核状态！", this.Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                }

            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("删除销售发货单"))
            {
                if ((int)(MessageBox.Show("您确定要删除改销售发货单吗？确定按YES.取消按NO", "提示", MessageBoxButtons.YesNo)) == 6)
                {
                    if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                    {
                        if (Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.danhao == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString() && x.Reconciliation == true) != null)
                        {
                            Tools.ShowMessageBox.ShowErrorBox("该单据已经存在已对账信息！不能直接删除\r\n请先取消对账状态");
                            return;
                        }
                        try
                        {
                            Connect.dbcontext.BeginTran();
                            销售发货单BLL.删除单据(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                            Connect.dbcontext.Insertable(new DeleteLog() { date = DateTime.Now, Log = $"删除销售发货单{gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()}", useID = Tools.CurrentUser.user.YHBH }).ExecuteCommand();
                            Connect.dbcontext.CommitTran();
                            Query();
                        }
                        catch (Exception ex)
                        {
                            Connect.dbcontext.RollbackTran();
                            ShowMessageBox.ShowErrorBox("删除的时候发送了错误：" + ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("该单据已经审核过，不能删除！要删除需先取消审核", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1, "销售发货单清单");
        }

        private void 审核通过ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("审核销售发货单"))
            {
                if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                {
                    销售发货单BLL.单据审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                    AlterDlg.Show("审核成功！");
                    检测状态.检测(danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()));
                    Query();
                }
                else
                {
                    MessageBox.Show("该单据已经审核过了！无需再次审核", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void 单据反审ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("反审核销售发货单"))
            {
                if (单据BLL.检查是否已经审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()))
                {
                    销售发货单BLL.单据反审核(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                    AlterDlg.Show("反审核成功！");
                    if (订单BLL.检查是否发完(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) == false)
                    {
                        订单BLL.重启订单(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString());
                    }
                    Query();
                }
                else
                {
                    MessageBox.Show("该单据还未审核通过！不能反审核", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = null, dt = gridControl1.DataSource as DataTable };
            fm.ShowDialog();
            if (!uiCheckBox1.Checked)
            {
                gridView1.Columns["hanshuidanjia"].Visible = false;
                gridView1.Columns["hanshuiheji"].Visible = false;
            }
            else
            {
                string password = "";
                this.ShowInputPasswordDialog(ref password);
                if (password == Tools.CurrentUser.user.MM)
                {
                    gridView1.Columns["hanshuidanjia"].Visible = true;
                    gridView1.Columns["hanshuiheji"].Visible = true;
                }
            }
        }

        private void txtksmc_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 客户选择() { linkman = new LXR() { MC = txtksmc.Text, ZJC = "" } };
            fm.ShowDialog();
            txtksmc.Text = fm.linkman.MC;
            uiCheckBox3.Checked = true;
            Query();
        }

        private void txtbianhao_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Query();
            }
        }

        private void 打包发货ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                var fm = new 打包发货() { Danhao = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString() };
                fm.Show();
            }
            else
            {
                AlterDlg.Show("没有任何可打包的单号");
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                MainForm.mainform.AddMidForm(new 销售发货单() { useful = FormUseful.查看, danju = DanjuTableService.GetOneDanjuTable(x => x.dh == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString()) });
            }
        }
        private void 复制单元格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridControl1.Focus();
            SendKeys.Send("^c");
        }

        private void uiCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (uiCheckBox1.Checked)
            {
                var a = GetAccess.AccessList.Where(x => x.AccessName == (this.Name + "价格可见")).ToList();
                if (a.Count > 0)
                {
                    if (a[0].Access == false)
                    {
                        Sunny.UI.UIMessageBox.ShowWarning("您没有权限查看销售价格！\r\n若要查看请与管理员联系！");
                        uiCheckBox1.Checked = false;
                        return;
                    }
                    else
                    {
                        string password = "";
                        this.ShowInputPasswordDialog(ref password);
                        if (password == Tools.CurrentUser.user.MM)
                        {
                            gridView1.Columns["hanshuidanjia"].Visible = true;
                            gridView1.Columns["hanshuiheji"].Visible = true;
                        }
                        else
                        {
                            Sunny.UI.UIMessageBox.ShowWarning("您输入的密码不对！");
                            uiCheckBox1.Checked = false;
                        }
                    }
                }
            }
            else
            {
                gridView1.Columns["hanshuidanjia"].Visible = false;
                gridView1.Columns["hanshuiheji"].Visible = false;
            }
        }

        private void 销售发货列表_Load(object sender, EventArgs e)
        {
            Query();
        }

        private void uiCheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            Query();
        }

        private void 上传图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                var fm = new 图片显示()
                {
                    DanjuBianhao = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString(),
                };
                fm.ShowDialog();
            }
        }

        private void 发货汇总ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("发货汇总") == true)
            {
                MainForm.mainform.AddMidForm(new 发货汇总());
            }
        }

        private void 合并码单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tools.ShowMessageBox.ShowInfoBox("还在制作中  ");
        }

        private void 填写价格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("销售价格填写"))
            {
                new 销价填写() { Danhao = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "dh").ToString() }.ShowDialog();
                Query();
            }
        }

        private void 已对账ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("标识对账"))
            {
                try
                {
                    Connect.dbcontext.BeginTran();
                    var selectrow = SelectHelper.GetSelect(gridView1);
                    var idstring = new GetIDHelper().GetDanjumingxiID(selectrow);
                    //foreach (DataRow  s in selectrow.Rows )
                    //{
                    //    Connect.dbcontext.Updateable<danjumingxitable>().SetColumns(x => x.Reconciliation == true).Where(x => x.ID ==s[ "ID1"].ObjToInt()).ExecuteCommand();
                    //}
                    Connect.dbcontext.Ado.ExecuteCommand($"Update danjumingxitable set Reconciliation='true' where id in ({idstring})");
                    Connect.dbcontext.CommitTran();
                    Query();
                }
                catch (Exception ex)
                {
                    Connect.dbcontext.RollbackTran();
                    Tools.ShowMessageBox.ShowErrorBox("标识已对账的时候发生了错误！" + ex.Message);
                }
            }
        }

        private void 取消已对账ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("取消对账标识") == true)
            {
                try
                {
                    Connect.dbcontext.BeginTran();
                    var selectrow = SelectHelper.GetSelect(gridView1);
                    var idstring = new GetIDHelper().GetDanjumingxiID(selectrow);
                    //foreach (DataRow s in selectrow.Rows)
                    //{
                    //    Connect.dbcontext.Updateable<danjumingxitable>().SetColumns(x => x.Reconciliation == false).Where(x => x.ID == s["ID1"].ObjToInt()).ExecuteCommand();
                    //}
                    Connect.dbcontext.Ado.ExecuteCommand($"Update danjumingxitable set Reconciliation='false' where id in ({idstring})");
                    Connect.dbcontext.CommitTran();
                    Query();
                }
                catch (Exception ex)
                {
                    Connect.dbcontext.RollbackTran();
                    Tools.ShowMessageBox.ShowErrorBox("标识已对账的时候发生了错误！" + ex.Message);
                }
            }
        }

        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintList(PrintModel.Privew);
        }
        private void PrintList(PrintModel printModel)
        {
            try
            {
                if (gridView1.GetSelectedRows().Length > 0)
                {
                    var dt = gridControl1.DataSource as DataTable;
                    var selectrow = SelectHelper.GetSelect(gridView1);
                    List<string> customers = new List<string>();
                    List<string> danhaos = new List<string>();
                    foreach (DataRow rowindex in selectrow.Rows)
                    {
                        customers.Add(rowindex["ksmc"].ToString());
                        danhaos.Add(rowindex["dh"].ToString());
                    }
                    selectrow.TableName = "单据信息";

                    var ds = new DataSet();
                    ds.Tables.Add(selectrow);
                    var customersDatatable = new DataTable();
                    customersDatatable.Columns.Add("客户名称");
                    var customer = string.Empty;
                    foreach (var col in customers.Distinct().ToList())
                    {
                        customer += "/" + col;
                    }
                    customersDatatable.Rows.Add(customer);
                    customersDatatable.TableName = "客户名称表";
                    ds.Tables.Add(customersDatatable);
                    ds.Tables.Add(GetFee(danhaos));
                    //var danjutable = JsonConvert.DeserializeObject<DataTable>(JsonConvert.SerializeObject (new GetIDHelper().GetDanjuTablesFromDocNums(new GetIDHelper().GetDanjumingxiDocNum (selectrow))));
                    var danjutable = new GetIDHelper().GetTablesFromDocNums(new GetIDHelper().GetDanjumingxiDocNum(selectrow));
                    danjutable.TableName = "码单单据";
                    var datedt = new DataTable();
                    datedt.Columns.Add("起始日期", typeof(DateTime));
                    datedt.Columns.Add("结束日期", typeof(DateTime));
                    datedt.TableName = "日期区间";
                    datedt.Rows.Add();
                    datedt.Rows[0][0] = dateEdit1.DateTime;
                    datedt.Rows[0][1] = dateEdit2.DateTime;
                    ds.Tables.Add(datedt);
                    ds.Tables.Add(danjutable.Copy());

                    PrintHelper.PrintReport(printModel, ds, PrintPath.报表模板 + "发货清单.frx");

                    if (Tools.ShowMessageBox.ShowAskBox("是否把打印的单据标识为已记账？") == DialogResult.Yes)
                    {
                        已对账ToolStripMenuItem_Click(null, null);
                    }
                }
            }
            catch (Exception ex)
            { ShowMessageBox.ShowErrorBox(ex.Message); }
        }

        private DataTable GetFee(List<string> danhaos)
        {
            var d = string.Empty;
            foreach (var danhao in danhaos)
            {
                d += "'" + danhao + "',";
            }
            d = d.Substring(0, d.Length - 2);
            var danjus = Connect.dbcontext.Ado.SqlQuery<DanjuTable>($"select * from danjutable where dh in ({d}')");
            DataTable dt = new DataTable();
            dt.Columns.Add("运费", typeof(decimal));
            dt.Columns.Add("装卸费", typeof(decimal));
            dt.Columns.Add("包装费", typeof(decimal));
            dt.Columns.Add("花辊费", typeof(decimal));
            dt.Columns.Add("其它费用", typeof(decimal));
            DataRow row = dt.NewRow();
            row["运费"] = danjus.Sum(x => x.yunfei);
            row["装卸费"] = danjus.Sum(x => x.StevedoringChargesFree);
            row["包装费"] = danjus.Sum(x => x.PackagingFee);
            row["花辊费"] = danjus.Sum(x => x.FlowerRollerFee);
            row["其它费用"] = danjus.Sum(x => x.OtherFees);
            dt.Rows.Add(row);
            dt.TableName = "其他费用";
            return dt;
        }
        private void 打印编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintList(PrintModel.Design);
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (AccessBLL.CheckAccess("修改发货清单单价"))
            {
                var id = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID1").ObjToInt();
                var mingxi = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.ID == id);
                if (mingxi != null)
                {
                    try
                    {
                        Connect.dbcontext.BeginTran();
                        var olddanju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == mingxi.danhao);
                        来往明细BLL.删除来往记录(olddanju);
                        财务BLL.减少应收款(olddanju);
                        财务BLL.减少应开发票(olddanju, Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == mingxi.danhao).ToList());
                        mingxi.hanshuidanjia = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "hanshuidanjia").ObjToDecimal();
                        mingxi.hanshuiheji = mingxi.hanshuidanjia * mingxi.toupimishu;
                        Connect.dbcontext.Updateable(mingxi).ExecuteCommand();
                        olddanju.je = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == mingxi.danhao).Sum(x => x.hanshuiheji);
                        Connect.dbcontext.Updateable(olddanju).ExecuteCommand();
                        来往明细BLL.增加来往记录(olddanju);
                        财务BLL.增加应收款(olddanju);
                        财务BLL.增加应开发票(Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == mingxi.danhao).ToList(), olddanju);
                        Connect.dbcontext.CommitTran();
                        Query();
                    }
                    catch (Exception ex)
                    {
                        Connect.dbcontext.RollbackTran();
                        Tools.ShowMessageBox.ShowErrorBox("保存的时候发生了错误!" + ex.Message);
                    }
                }
            }
        }
        private void 相同单价ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("修改发货清单单价"))
            {
                double price = 0;
                Sunny.UI.UIInputDialog.ShowInputDoubleDialog(this, ref price, 2, true, "请输入单价");
                try
                {
                    this.ShowWaitForm();
                    Connect.dbcontext.BeginTran();
                    foreach (DataRow row in SelectHelper.GetSelect(gridView1).Rows)
                    {
                        var m = Connect.dbcontext.Queryable<danjumingxitable>().First(x => x.ID == row["ID1"].ObjToInt());
                        var olddanju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == m.danhao);
                        来往明细BLL.删除来往记录(olddanju);
                        财务BLL.减少应收款(olddanju);
                        财务BLL.减少应开发票(olddanju, Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == m.danhao).ToList());
                        m.hanshuidanjia = (decimal)price;
                        m.hanshuiheji = m.hanshuidanjia * m.toupimishu;
                        Connect.dbcontext.Updateable(m).ExecuteCommand();
                        olddanju.je = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == m.danhao).Sum(x => x.hanshuiheji);
                        Connect.dbcontext.Updateable(olddanju).ExecuteCommand();
                        来往明细BLL.增加来往记录(olddanju);
                        财务BLL.增加应收款(olddanju);
                        财务BLL.增加应开发票(Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == m.danhao).ToList(), olddanju);
                    }
                    Connect.dbcontext.CommitTran();
                    Query();
                    this.HideWaitForm();
                }
                catch (Exception ex)
                {
                    this.HideWaitForm();
                    Connect.dbcontext.RollbackTran();
                    Tools.ShowMessageBox.ShowErrorBox($"Error: {ex.Message}");
                }
            }
        }

        private void 生成对账单ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            // 假设我们基于某个条件来设置颜色
            if (e.RowHandle >= 0 && e.RowHandle < gridView1.DataRowCount)
            {
                // 获取当前行的数据
                DataRow row = gridView1.GetDataRow(e.RowHandle);
                // 假设我们根据某个字段的值来设置颜色
                string someFieldValue = row["Reconciliation"].ToString();

                // 设置背景色和字体色
                if (row["Reconciliation"].ObjToBool() == true)
                {
                    //e.Appearance.BackColor = Color.CornflowerBlue; // 设置背景色
                    e.Appearance.ForeColor = Color.Red;
                }
                if(row["djlx"].ToString() == DanjuLeiXing.销售预出库 )
                {
                    e.Appearance.BackColor = Color.CornflowerBlue; // 设置背景色
                }
            }
        }
    }
}

